// SPDX-License-Identifier: (GPL-2.0+ OR MIT)
/*
 * Copyright (c) 2019 BayLibre, SAS
 * Author: Neil Armstrong <narmstrong@baylibre.com>
 */

/dts-v1/;

#include "meson-g12b-s922x.dtsi"
#include <dt-bindings/input/input.h>
#include <dt-bindings/gpio/meson-g12a-gpio.h>
#include <dt-bindings/sound/meson-g12a-toacodec.h>
#include <dt-bindings/sound/meson-g12a-tohdmitx.h>

/ {
	compatible = "hardkernel,odroid-n2", "amlogic,s922x", "amlogic,g12b";
	model = "Hardkernel ODROID-N2";

	aliases {
		serial0 = &uart_AO;
		ethernet0 = &ethmac;
	};

	dioo2133: audio-amplifier-0 {
		compatible = "simple-audio-amplifier";
		enable-gpios = <&gpio_ao GPIOAO_2 GPIO_ACTIVE_HIGH>;
		VCC-supply = <&vcc_5v>;
		sound-name-prefix = "U19";
		status = "okay";
	};

	chosen {
		stdout-path = "serial0:115200n8";
	};

	memory@0 {
		device_type = "memory";
		reg = <0x0 0x0 0x0 0x40000000>;
	};

	emmc_pwrseq: emmc-pwrseq {
		compatible = "mmc-pwrseq-emmc";
		reset-gpios = <&gpio BOOT_12 GPIO_ACTIVE_LOW>;
	};

	leds {
		compatible = "gpio-leds";

		blue {
			label = "n2:blue";
			gpios = <&gpio_ao GPIOAO_11 GPIO_ACTIVE_HIGH>;
			linux,default-trigger = "heartbeat";
		};
	};

	tflash_vdd: regulator-tflash_vdd {
		compatible = "regulator-fixed";

		regulator-name = "TFLASH_VDD";
		regulator-min-microvolt = <3300000>;
		regulator-max-microvolt = <3300000>;

		gpio = <&gpio_ao GPIOAO_8 GPIO_ACTIVE_HIGH>;
		enable-active-high;
		regulator-always-on;
	};

	tf_io: gpio-regulator-tf_io {
		compatible = "regulator-gpio";

		regulator-name = "TF_IO";
		regulator-min-microvolt = <1800000>;
		regulator-max-microvolt = <3300000>;

		gpios = <&gpio_ao GPIOAO_9 GPIO_ACTIVE_HIGH>;
		gpios-states = <0>;

		states = <3300000 0>,
			 <1800000 1>;
	};

	flash_1v8: regulator-flash_1v8 {
		compatible = "regulator-fixed";
		regulator-name = "FLASH_1V8";
		regulator-min-microvolt = <1800000>;
		regulator-max-microvolt = <1800000>;
		vin-supply = <&vcc_3v3>;
		regulator-always-on;
	};

	main_12v: regulator-main_12v {
		compatible = "regulator-fixed";
		regulator-name = "12V";
		regulator-min-microvolt = <12000000>;
		regulator-max-microvolt = <12000000>;
		regulator-always-on;
	};

	vcc_5v: regulator-vcc_5v {
		compatible = "regulator-fixed";
		regulator-name = "5V";
		regulator-min-microvolt = <5000000>;
		regulator-max-microvolt = <5000000>;
		regulator-always-on;
		vin-supply = <&main_12v>;
	};

	vcc_1v8: regulator-vcc_1v8 {
		compatible = "regulator-fixed";
		regulator-name = "VCC_1V8";
		regulator-min-microvolt = <1800000>;
		regulator-max-microvolt = <1800000>;
		vin-supply = <&vcc_3v3>;
		regulator-always-on;
	};

	vcc_3v3: regulator-vcc_3v3 {
		compatible = "regulator-fixed";
		regulator-name = "VCC_3V3";
		regulator-min-microvolt = <3300000>;
		regulator-max-microvolt = <3300000>;
		vin-supply = <&vddao_3v3>;
		regulator-always-on;
		/* FIXME: actually controlled by VDDCPU_B_EN */
	};

	vddcpu_a: regulator-vddcpu-a {
		/*
		 * MP8756GD Regulator.
		 */
		compatible = "pwm-regulator";

		regulator-name = "VDDCPU_A";
		regulator-min-microvolt = <721000>;
		regulator-max-microvolt = <1022000>;

		vin-supply = <&main_12v>;

		pwms = <&pwm_ab 0 1250 0>;
		pwm-dutycycle-range = <100 0>;

		regulator-boot-on;
		regulator-always-on;
	};

	vddcpu_b: regulator-vddcpu-b {
		/*
		 * Silergy SY8120B1ABC Regulator.
		 */
		compatible = "pwm-regulator";

		regulator-name = "VDDCPU_B";
		regulator-min-microvolt = <721000>;
		regulator-max-microvolt = <1022000>;

		vin-supply = <&main_12v>;

		pwms = <&pwm_AO_cd 1 1250 0>;
		pwm-dutycycle-range = <100 0>;

		regulator-boot-on;
		regulator-always-on;
	};

	hub_5v: regulator-hub_5v {
		compatible = "regulator-fixed";
		regulator-name = "HUB_5V";
		regulator-min-microvolt = <5000000>;
		regulator-max-microvolt = <5000000>;
		vin-supply = <&vcc_5v>;

		/* Connected to the Hub CHIPENABLE, LOW sets low power state */
		gpio = <&gpio GPIOH_5 GPIO_ACTIVE_HIGH>;
		enable-active-high;
	};

	usb_pwr_en: regulator-usb_pwr_en {
		compatible = "regulator-fixed";
		regulator-name = "USB_PWR_EN";
		regulator-min-microvolt = <5000000>;
		regulator-max-microvolt = <5000000>;
		vin-supply = <&vcc_5v>;

		/* Connected to the microUSB port power enable */
		gpio = <&gpio GPIOH_6 GPIO_ACTIVE_HIGH>;
		enable-active-high;
	};

	vddao_1v8: regulator-vddao_1v8 {
		compatible = "regulator-fixed";
		regulator-name = "VDDAO_1V8";
		regulator-min-microvolt = <1800000>;
		regulator-max-microvolt = <1800000>;
		vin-supply = <&vddao_3v3>;
		regulator-always-on;
	};

	vddao_3v3: regulator-vddao_3v3 {
		compatible = "regulator-fixed";
		regulator-name = "VDDAO_3V3";
		regulator-min-microvolt = <3300000>;
		regulator-max-microvolt = <3300000>;
		vin-supply = <&main_12v>;
		regulator-always-on;
	};

	hdmi-connector {
		compatible = "hdmi-connector";
		type = "a";

		port {
			hdmi_connector_in: endpoint {
				remote-endpoint = <&hdmi_tx_tmds_out>;
			};
		};
	};

	sound {
		compatible = "amlogic,axg-sound-card";
		model = "G12B-ODROID-N2";
		audio-widgets = "Line", "Lineout";
		audio-aux-devs = <&tdmout_b>, <&tdmout_c>, <&tdmin_a>,
				 <&tdmin_b>, <&tdmin_c>, <&tdmin_lb>,
				 <&dioo2133>;
		audio-routing = "TDMOUT_B IN 0", "FRDDR_A OUT 1",
				"TDMOUT_B IN 1", "FRDDR_B OUT 1",
				"TDMOUT_B IN 2", "FRDDR_C OUT 1",
				"TDM_B Playback", "TDMOUT_B OUT",
				"TDMOUT_C IN 0", "FRDDR_A OUT 2",
				"TDMOUT_C IN 1", "FRDDR_B OUT 2",
				"TDMOUT_C IN 2", "FRDDR_C OUT 2",
				"TDM_C Playback", "TDMOUT_C OUT",
				"TDMIN_A IN 4", "TDM_B Loopback",
				"TDMIN_B IN 4", "TDM_B Loopback",
				"TDMIN_C IN 4", "TDM_B Loopback",
				"TDMIN_LB IN 1", "TDM_B Loopback",
				"TDMIN_A IN 5", "TDM_C Loopback",
				"TDMIN_B IN 5", "TDM_C Loopback",
				"TDMIN_C IN 5", "TDM_C Loopback",
				"TDMIN_LB IN 2", "TDM_C Loopback",
				"TODDR_A IN 0", "TDMIN_A OUT",
				"TODDR_B IN 0", "TDMIN_A OUT",
				"TODDR_C IN 0", "TDMIN_A OUT",
				"TODDR_A IN 1", "TDMIN_B OUT",
				"TODDR_B IN 1", "TDMIN_B OUT",
				"TODDR_C IN 1", "TDMIN_B OUT",
				"TODDR_A IN 2", "TDMIN_C OUT",
				"TODDR_B IN 2", "TDMIN_C OUT",
				"TODDR_C IN 2", "TDMIN_C OUT",
				"TODDR_A IN 6", "TDMIN_LB OUT",
				"TODDR_B IN 6", "TDMIN_LB OUT",
				"TODDR_C IN 6", "TDMIN_LB OUT",
				"U19 INL", "ACODEC LOLP",
				"U19 INR", "ACODEC LORP",
				"Lineout", "U19 OUTL",
				"Lineout", "U19 OUTR";

		assigned-clocks = <&clkc CLKID_MPLL2>,
				  <&clkc CLKID_MPLL0>,
				  <&clkc CLKID_MPLL1>;
		assigned-clock-parents = <0>, <0>, <0>;
		assigned-clock-rates = <294912000>,
				       <270950400>,
				       <393216000>;
		status = "okay";

		dai-link-0 {
			sound-dai = <&frddr_a>;
		};

		dai-link-1 {
			sound-dai = <&frddr_b>;
		};

		dai-link-2 {
			sound-dai = <&frddr_c>;
		};

		dai-link-3 {
			sound-dai = <&toddr_a>;
		};

		dai-link-4 {
			sound-dai = <&toddr_b>;
		};

		dai-link-5 {
			sound-dai = <&toddr_c>;
		};

		/* 8ch hdmi interface */
		dai-link-6 {
			sound-dai = <&tdmif_b>;
			dai-format = "i2s";
			dai-tdm-slot-tx-mask-0 = <1 1>;
			dai-tdm-slot-tx-mask-1 = <1 1>;
			dai-tdm-slot-tx-mask-2 = <1 1>;
			dai-tdm-slot-tx-mask-3 = <1 1>;
			mclk-fs = <256>;

			codec-0 {
				sound-dai = <&tohdmitx TOHDMITX_I2S_IN_B>;
			};

			codec-1 {
				sound-dai = <&toacodec TOACODEC_IN_B>;
			};
		};

		/* i2s jack output interface */
		dai-link-7 {
			sound-dai = <&tdmif_c>;
			dai-format = "i2s";
			dai-tdm-slot-tx-mask-0 = <1 1>;
			mclk-fs = <256>;

			codec-0 {
				sound-dai = <&tohdmitx TOHDMITX_I2S_IN_C>;
			};

			codec-1 {
				sound-dai = <&toacodec TOACODEC_IN_C>;
			};
		};

		/* hdmi glue */
		dai-link-8 {
			sound-dai = <&tohdmitx TOHDMITX_I2S_OUT>;

			codec {
				sound-dai = <&hdmi_tx>;
			};
		};

		/* acodec glue */
		dai-link-9 {
			sound-dai = <&toacodec TOACODEC_OUT>;

			codec {
				sound-dai = <&acodec>;
			};
		};
	};
};

&acodec {
	AVDD-supply = <&vddao_1v8>;
	status = "okay";
};

&arb {
	status = "okay";
};

&cec_AO {
	pinctrl-0 = <&cec_ao_a_h_pins>;
	pinctrl-names = "default";
	status = "disabled";
	hdmi-phandle = <&hdmi_tx>;
};

&cecb_AO {
	pinctrl-0 = <&cec_ao_b_h_pins>;
	pinctrl-names = "default";
	status = "okay";
	hdmi-phandle = <&hdmi_tx>;
};

&clkc_audio {
	status = "okay";
};

&cpu0 {
	cpu-supply = <&vddcpu_b>;
	operating-points-v2 = <&cpu_opp_table_0>;
	clocks = <&clkc CLKID_CPU_CLK>;
	clock-latency = <50000>;
};

&cpu1 {
	cpu-supply = <&vddcpu_b>;
	operating-points-v2 = <&cpu_opp_table_0>;
	clocks = <&clkc CLKID_CPU_CLK>;
	clock-latency = <50000>;
};

&cpu100 {
	cpu-supply = <&vddcpu_a>;
	operating-points-v2 = <&cpub_opp_table_1>;
	clocks = <&clkc CLKID_CPUB_CLK>;
	clock-latency = <50000>;
};

&cpu101 {
	cpu-supply = <&vddcpu_a>;
	operating-points-v2 = <&cpub_opp_table_1>;
	clocks = <&clkc CLKID_CPUB_CLK>;
	clock-latency = <50000>;
};

&cpu102 {
	cpu-supply = <&vddcpu_a>;
	operating-points-v2 = <&cpub_opp_table_1>;
	clocks = <&clkc CLKID_CPUB_CLK>;
	clock-latency = <50000>;
};

&cpu103 {
	cpu-supply = <&vddcpu_a>;
	operating-points-v2 = <&cpub_opp_table_1>;
	clocks = <&clkc CLKID_CPUB_CLK>;
	clock-latency = <50000>;
};

&ext_mdio {
	external_phy: ethernet-phy@0 {
		/* Realtek RTL8211F (0x001cc916) */	
		reg = <0>;
		max-speed = <1000>;

		reset-assert-us = <10000>;
		reset-deassert-us = <30000>;
		reset-gpios = <&gpio GPIOZ_15 (GPIO_ACTIVE_LOW | GPIO_OPEN_DRAIN)>;

		interrupt-parent = <&gpio_intc>;
		/* MAC_INTR on GPIOZ_14 */
		interrupts = <26 IRQ_TYPE_LEVEL_LOW>;
	};
};

&ethmac {
	pinctrl-0 = <&eth_pins>, <&eth_rgmii_pins>;
	pinctrl-names = "default";
	status = "okay";
	phy-mode = "rgmii";
	phy-handle = <&external_phy>;
	amlogic,tx-delay-ns = <2>;
};

&frddr_a {
	status = "okay";
};

&frddr_b {
	status = "okay";
};

&frddr_c {
	status = "okay";
};

&gpio {
	/*
	 * WARNING: The USB Hub on the Odroid-N2 needs a reset signal
	 * to be turned high in order to be detected by the USB Controller
	 * This signal should be handled by a USB specific power sequence
	 * in order to reset the Hub when USB bus is powered down.
	 */
	usb-hub {
		gpio-hog;
		gpios = <GPIOH_4 GPIO_ACTIVE_HIGH>;
		output-high;
		line-name = "usb-hub-reset";
	};
};

&hdmi_tx {
	status = "okay";
	pinctrl-0 = <&hdmitx_hpd_pins>, <&hdmitx_ddc_pins>;
	pinctrl-names = "default";
	hdmi-supply = <&vcc_5v>;
};

&hdmi_tx_tmds_port {
	hdmi_tx_tmds_out: endpoint {
		remote-endpoint = <&hdmi_connector_in>;
	};
};

&ir {
	status = "okay";
	pinctrl-0 = <&remote_input_ao_pins>;
	pinctrl-names = "default";
	linux,rc-map-name = "rc-odroid";
};

&pwm_ab {
	pinctrl-0 = <&pwm_a_e_pins>;
	pinctrl-names = "default";
	clocks = <&xtal>;
	clock-names = "clkin0";
	status = "okay";
};

&pwm_AO_cd {
	pinctrl-0 = <&pwm_ao_d_e_pins>;
	pinctrl-names = "default";
	clocks = <&xtal>;
	clock-names = "clkin1";
	status = "okay";
};

/* SD card */
&sd_emmc_b {
	status = "okay";
	pinctrl-0 = <&sdcard_c_pins>;
	pinctrl-1 = <&sdcard_clk_gate_c_pins>;
	pinctrl-names = "default", "clk-gate";

	bus-width = <4>;
	cap-sd-highspeed;
	max-frequency = <50000000>;
	disable-wp;

	cd-gpios = <&gpio GPIOC_6 GPIO_ACTIVE_LOW>;
	vmmc-supply = <&tflash_vdd>;
	vqmmc-supply = <&tf_io>;

};

/* eMMC */
&sd_emmc_c {
	status = "okay";
	pinctrl-0 = <&emmc_ctrl_pins>, <&emmc_data_8b_pins>, <&emmc_ds_pins>;
	pinctrl-1 = <&emmc_clk_gate_pins>;
	pinctrl-names = "default", "clk-gate";

	bus-width = <8>;
	cap-mmc-highspeed;
	mmc-ddr-1_8v;
	mmc-hs200-1_8v;
	max-frequency = <200000000>;
	disable-wp;

	mmc-pwrseq = <&emmc_pwrseq>;
	vmmc-supply = <&vcc_3v3>;
	vqmmc-supply = <&flash_1v8>;
};

/*
 * EMMC_D4, EMMC_D5, EMMC_D6 and EMMC_D7 pins are shared between SPI NOR pins
 * and eMMC Data 4 to 7 pins.
 * Replace emmc_data_8b_pins to emmc_data_4b_pins from sd_emmc_c pinctrl-0,
 * and change bus-width to 4 then spifc can be enabled.
 * The SW1 slide should also be set to the correct position.
 */
&spifc {
	status = "disabled";
	pinctrl-0 = <&nor_pins>;
	pinctrl-names = "default";

	mx25u64: spi-flash@0 {
		#address-cells = <1>;
		#size-cells = <1>;
		compatible = "mxicy,mx25u6435f", "jedec,spi-nor";
		reg = <0>;
		spi-max-frequency = <104000000>;
	};
};

&tdmif_b {
	status = "okay";
};

&tdmif_c {
	status = "okay";
};

&tdmin_a {
	status = "okay";
};

&tdmin_b {
	status = "okay";
};

&tdmin_c {
	status = "okay";
};

&tdmin_lb {
	status = "okay";
};

&tdmout_b {
	status = "okay";
};

&tdmout_c {
	status = "okay";
};

&toacodec {
	status = "okay";
};

&tohdmitx {
	status = "okay";
};

&toddr_a {
	status = "okay";
};

&toddr_b {
	status = "okay";
};

&toddr_c {
	status = "okay";
};

&uart_AO {
	status = "okay";
	pinctrl-0 = <&uart_ao_a_pins>;
	pinctrl-names = "default";
};

&usb {
	status = "okay";
	vbus-supply = <&usb_pwr_en>;
};

&usb2_phy0 {
	phy-supply = <&vcc_5v>;
};

&usb2_phy1 {
	/* Enable the hub which is connected to this port */
	phy-supply = <&hub_5v>;
};
